package com.irdstudio.efp.console.service.impl;

import java.math.BigDecimal;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.console.service.dao.CoeffCfgDao;
import com.irdstudio.efp.console.service.domain.CoeffCfg;
import com.irdstudio.efp.console.service.facade.CoeffCfgService;
import com.irdstudio.efp.console.service.vo.CoeffCfgVO;

/**
 * <p>
 * 提额系数配置表的Service实现类
 * </p>
 * @author szbd
 * @createDate 2018-09-17 15:10:09
 */
@Service("coeffCfgService")
public class CoeffCfgServiceImpl implements CoeffCfgService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(CoeffCfgServiceImpl.class);
    
    @Autowired
    private CoeffCfgDao coeffCfgDao;
    
    @Override
    public List<CoeffCfgVO> queryAllOwner(CoeffCfgVO inCoeffCfgVo) {
        logger.info("查询本人所属的提额系数配置表记录的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        List<CoeffCfgVO> resultList =null;
        String flag = inCoeffCfgVo.getFlag();
        try {
        	List<CoeffCfg> dataList = null;
        	if ("disPlay".equals(flag)) {
				dataList = coeffCfgDao.queryAllOwner(inCoeffCfgVo); 
				resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
			}else {
				dataList = coeffCfgDao.queryAllOwnerByPage(inCoeffCfgVo);
				if (Objects.nonNull(dataList)) {
					resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
					pageSet(resultList, inCoeffCfgVo);
				}else {
				logger.info("查询本人所属的提额系数配置表记录为空！");
			}
            }
        	return resultList;
        } catch (Exception e) {
            logger.error("查询本人所属的提额系数配置表记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<CoeffCfgVO> queryAllCurrOrg(CoeffCfgVO inCoeffCfgVo) {
        logger.info("查询本机构的提额系数配置表记录的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
       
        List<CoeffCfgVO> resultList =null;
        String flag = inCoeffCfgVo.getFlag();
        try {
        	List<CoeffCfg> dataList = null;
        	if ("disPlay".equals(flag)) {
				dataList = coeffCfgDao.queryAllCurrOrg(inCoeffCfgVo); 
				resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
			}else {
				dataList = coeffCfgDao.queryAllCurrOrgByPage(inCoeffCfgVo);
				if (Objects.nonNull(dataList)) {
					resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
					pageSet(resultList, inCoeffCfgVo);
				}else {
				logger.info("查询本机构的提额系数配置表记录为空！");
			}
            }
        	return resultList;
        } catch (Exception e) {
            logger.error("查询本机构的提额系数配置表记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<CoeffCfgVO> queryAllCurrDownOrg(CoeffCfgVO inCoeffCfgVo) {
        logger.info("查询本机构及下属机构的提额系数配置表记录的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        List<CoeffCfgVO> resultList =null;
        String flag = inCoeffCfgVo.getFlag();
        try {
        	List<CoeffCfg> dataList = null;
        	if ("disPlay".equals(flag)) {
				dataList = coeffCfgDao.queryAllCurrDownOrg(inCoeffCfgVo); 
				resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
			}else {
				dataList = coeffCfgDao.queryAllCurrDownOrgByPage(inCoeffCfgVo);
				if (Objects.nonNull(dataList)) {
					resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
					pageSet(resultList, inCoeffCfgVo);
				}else {
				logger.info("查询本机构及下属机构的提额系数配置表记录为空！");
			}
            }
        	return resultList;
        } catch (Exception e) {
            logger.error("查询本机构及下属机构的提额系数配置表记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public int insertCoeffCfg(CoeffCfgVO inCoeffCfgVo) {
        logger.info("新增提额系数配置表数据的参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return 0;
        }
        int count = 0;
        try {
            CoeffCfg coeffCfg = new CoeffCfg();
            beanCopy(inCoeffCfgVo, coeffCfg);
            count = coeffCfgDao.insertCoeffCfg(coeffCfg);
        } catch (Exception e) {
            logger.error("新增提额系数配置表数据发生异常！", e);
            count = -1;
        }
        logger.info("当前新增提额系数配置表数据条数为：" + count);
        return count;
    }
    
    @Override
    public int deleteCoeffCfgByPk(CoeffCfgVO inCoeffCfgVo) {
        logger.info("删除提额系数配置表数据的参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return 0;
        }
        int count = 0;
        try {
            CoeffCfg coeffCfg = new CoeffCfg();
            beanCopy(inCoeffCfgVo, coeffCfg);
            count = coeffCfgDao.deleteCoeffCfgByPk(coeffCfg);
        } catch (Exception e) {
            logger.error("删除提额系数配置表数据发生异常！", e);
            count = -1;
        }
        logger.info("删除的提额系数配置表数据条数为：" + count);
        return count;
    }
    
    @Override
    public int updateCoeffCfgByPk(CoeffCfgVO inCoeffCfgVo) {
        logger.info("修改提额系数配置表数据的参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return 0;
        }
        int count = 0;
        try {
            CoeffCfg coeffCfg = new CoeffCfg();
            beanCopy(inCoeffCfgVo, coeffCfg);
            count = coeffCfgDao.updateCoeffCfgByPk(coeffCfg);
        } catch (Exception e) {
            logger.error("修改数据发生异常！", e);
            count = -1;
        }
        logger.info("修改的提额系数配置表数据条数为" + count);
        return count;
    }
    
    @Override
    public CoeffCfgVO queryCoeffCfgByPk(CoeffCfgVO inCoeffCfgVo) {
        logger.info("根据主键查询提额系数配置表的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        try {
            CoeffCfg coeffCfg = new CoeffCfg();
            beanCopy(inCoeffCfgVo, coeffCfg);
            coeffCfg = coeffCfgDao.queryCoeffCfgByPk(coeffCfg);
            if (Objects.nonNull(coeffCfg)) {
                CoeffCfgVO outCoeffCfgVo = beanCopy(coeffCfg, new CoeffCfgVO());
                return outCoeffCfgVo;
            } else {
                logger.info("根据主键查询提额系数配置表的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据主键查询提额系数配置表记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public CoeffCfgVO querySingleCoeffCfgByCondition(CoeffCfgVO inCoeffCfgVo) {
        logger.info("根据条件查询单笔提额系数配置表的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        try {
            CoeffCfg coeffCfg = new CoeffCfg();
            beanCopy(inCoeffCfgVo, coeffCfg);
            coeffCfg = coeffCfgDao.querySingleCoeffCfgByCondition(coeffCfg);
            if (Objects.nonNull(coeffCfg)) {
                CoeffCfgVO outCoeffCfgVo = beanCopy(coeffCfg, new CoeffCfgVO());
                return outCoeffCfgVo;
            } else {
                logger.info("根据条件查询单笔提额系数配置表的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据条件查询单笔提额系数配置表记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<CoeffCfgVO> queryCoeffCfgByCondition(CoeffCfgVO inCoeffCfgVo) {
        logger.info("条件查询提额系数配置表的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        try {
            List<CoeffCfg> dataList = coeffCfgDao.queryCoeffCfgByCondition(inCoeffCfgVo);
            if (Objects.nonNull(dataList)) {
                List<CoeffCfgVO> resultList = new ArrayList<CoeffCfgVO>();
                resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
                return resultList;
            } else {
                logger.info("条件查询提额系数配置表的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件查询提额系数配置表发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<CoeffCfgVO> queryCoeffCfgByConditionByPage(CoeffCfgVO inCoeffCfgVo) {
        logger.info("条件分页查询提额系数配置表的请求参数为：" + JSONObject.toJSONString(inCoeffCfgVo));
        if(Objects.isNull(inCoeffCfgVo)){
            return null;
        }
        try {
            List<CoeffCfg> dataList = coeffCfgDao.queryCoeffCfgByConditionByPage(inCoeffCfgVo);
            if (Objects.nonNull(dataList)) {
                List<CoeffCfgVO> resultList = new ArrayList<CoeffCfgVO>();
                resultList = (List<CoeffCfgVO>) beansCopy(dataList, CoeffCfgVO.class);
                return resultList;
            } else {
                logger.info("条件分页查询提额系数配置表的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件分页查询提额系数配置表发生异常！", e);
        }
        return null;
    }

    @Override
    public Map<String, BigDecimal> queryCoffByLegalTypes(List<String> typeCodes, String legalCode) {
        if ( null == typeCodes || typeCodes.size() == 0 ) return null;
        Map<String, BigDecimal> param = new HashMap<>(typeCodes.size());
        List<CoeffCfg> coeffCfgs = coeffCfgDao.queryAllByTypesAndLegal(typeCodes, legalCode);
        if ( null != coeffCfgs && coeffCfgs.size() > 0 ) {
            for(CoeffCfg coeffCfg : coeffCfgs) {
                param.put(coeffCfg.getTypeId(), coeffCfg.getCoefficient());
                for (int i = 0; i < typeCodes.size(); i++) {
                    if (typeCodes.get(i).equals(coeffCfg.getTypeId())){
                        typeCodes.remove(i);
                        break;
                    }
                }
            }
        }
        //如果机构的没有配置，则查询省级配置的系数
        if (typeCodes.size() > 0 ) {
            List<CoeffCfg> provCoeffCfgs = coeffCfgDao.queryAllByTypesAndLegal(typeCodes, "00001");
            if ( null != provCoeffCfgs && provCoeffCfgs.size() > 0 ) {
                for(CoeffCfg coeffCfg : provCoeffCfgs) {
                    param.put(coeffCfg.getTypeId(), coeffCfg.getCoefficient());
                }
            }
        }

        return param;
    }


}
